home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/perl -w
- # -------------------------------------------------------------------------------
- # dictionaries-common.config-base:
- # dc-debconf-select.pl will be added to the end of this file
- # to make dictionaries-common.config
- # -------------------------------------------------------------------------------
-
- use Debconf::Client::ConfModule q(:all);
-
- version ('2.0');
-
- if ( -l "/etc/dictionary" ) {
- input ("medium","dictionaries-common/old_wordlist_link");
- }
-
- if ( not -l "/usr/dict" ){
- set("dictionaries-common/remove_old_usr_dict_link","false");
- }
-
- go();
-
- if ( $ARGV[0] eq "reconfigure" ){
- my $flag_file = "/var/cache/dictionaries-common/postinst.reconfiguring";
- open (REC_FLAG,"> $flag_file") or die "Could not open $flag_file for writing";
- print REC_FLAG "We are reconfiguring the package...\n";
- close REC_FLAG;
- }
-
- # Trying to find a reasonable guess for default ispell dictionary and wordlist
- # from the debian-installer settings, envvars or pre-policy symlinks and the
- # list of ispell dictionaries and wordlists to be installed
-
- $priority{"ispell"} = "critical";
- $priority{"wordlist"} = "critical";
- $di_language = "debian-installer/language";
- $di_country = "debian-installer/country";
- $dcscript = "/usr/share/dictionaries-common/dc-debconf-select.pl";
- $fromdcconfig = "yes";
- $debug = "yes" if exists $ENV{'DICT_COMMON_DEBUG'};
-
-
- my %debconf_vals = ();
- my @suffixes = ("","-large","-medium","-small","-gut");
- my %equivs = ("bg" => "bulgarian",
- "ca" => "catalan",
- "cs" => "czech",
- "da" => "danish",
- "de" => "ngerman",
- "de:1" => "ogerman",
- "de_CH" => "swiss",
- "en_US" => "american",
- "en_US:1" => "miscfiles",
- "en_CA" => "canadian",
- "en_CA:1" => "american",
- "en_GB" => "british",
- "en_AU" => "british",
- "eo" => "esperanto",
- "es" => "spanish",
- "fi" => "finnish",
- "fo" => "faroese",
- "fr" => "french",
- "ga" => "irish",
- "gd" => "gaelic",
- "gl" => "galician-minimos",
- "gv" => "manx",
- "hu" => "hungarian",
- "it" => "italian",
- "lt" => "lithuanian",
- "nb" => "norwegian->bokmal",
- "nl" => "dutch",
- "nn" => "norwegian->nynorsk",
- "pl" => "polish",
- "pt" => "portuguese",
- "pt_BR" => "brazilian",
- "ru" => "russian",
- "sv" => "swedish",
- "tl" => "tagalog",
- "uk" => "ukrainian");
- my %pending_keys = ();
- my %reverse_equivs = ();
- my %alternatives = ("ispell" => "ispell-dictionary.hash",
- "wordlist" => "dictionary");
-
- # -------------------------------------------------------------
- sub dc_debugprint(){
- # -------------------------------------------------------------
- # Show info if in debug mode
- # -------------------------------------------------------------
- print STDERR "@_" if $debug;
- }
-
- # -------------------------------------------------------------
- sub dc_set (){
- # -------------------------------------------------------------
- # Set debconf value unless already set
- # -------------------------------------------------------------
- my $question = shift;
- my $value = shift;
- my $priority = $priority{$class} || "";
-
- my ($errorcode, $oldvalue) = get($question);
-
- $oldvalue = "unset" unless $oldvalue;
-
- if ( $errorcode or $oldvalue eq "unset" ){
- &dc_debugprint(" $question: errorcode: $errorcode; priority: $priority\n" .
- " Old:[$oldvalue] --> New:[$value]\n");
- set("$question","$value");
- } elsif ( $oldvalue eq $value ) {
- print STDERR "Info: $question is already set to
- [$oldvalue]. Preserving it.\n";
- } else {
- print STDERR "Warning: $question is already set to
- [$oldvalue].
- Not setting to [$value]\n";
- }
-
- if ( $debug ){ # --- Check if question value is actually set
- ($errorcode, $oldvalue) = get($question);
- if ( $errorcode ){
- print STDERR "dictionaries-common: $question reading failed with $errorcode\n";
- } elsif ( $oldvalue) {
- print STDERR "dictionaries-common: $question actually set to [$oldvalue]\n";
- } else {
- print STDERR "dictionaries-common: $question value is void, bad thing\n";
- }
- }
- &dc_debugprint ("Not tried: " . join(', ',sort keys %pending_keys) . "\n---\n");
- }
-
- # -------------------------------------------------------------
- sub extractlangname (){
- # -------------------------------------------------------------
- # Look if a dict matching $langkey in %equivs is to be installed
- # and return the preferred language name if so.
- # -------------------------------------------------------------
- my $langkey = shift;
- my $thestring = '';
- my $thepackage = '';
- my $thevariant = '';
- my @thevalues = ();
-
- if ( exists $pending_keys{$langkey} ){ # Make sure we do not try it again
- &dc_debugprint("Trying langkey $langkey\n");
- delete $pending_keys{$langkey};
- } else {
- if ( exists $equivs{$langkey} ){
- # This $langkey was already tried, no need to try it again
- &dc_debugprint("Already done langkey $langkey\n");
- } else {
- # This $langkey does not exist
- &dc_debugprint("Non-existant langkey $langkey\n");
- }
- return;
- }
-
- if ( exists $equivs{$langkey} ){
- ($thepackage,$thevariant) = split ("->",$equivs{$langkey});
- foreach $suffix ( @suffixes ){
- if ( $thepackage eq "miscfiles" ){
- $pkgfullname = "$thepackage$suffix";
- } else {
- $pkgfullname = "$classprefix$thepackage$suffix";
- }
- &dc_debugprint(" Trying package $pkgfullname\n");
- if ( exists $debconf_vals{"$pkgfullname"} ){
- if ( exists $debconf_defaultvals{"$pkgfullname"} ){
- $thestring = $debconf_defaultvals{"$pkgfullname"};
- } else {
- $thestring = $debconf_vals{"$pkgfullname"};
- }
- @thevalues = sort split (/\s*,\s*/,$thestring);
- if ( $thevariant ){
- @thevalues = grep {/$thevariant/i} @thevalues;
- }
- @thevalues = sort {
- $a =~ m/tex/i <=> $b =~ m/tex/i # Sort tex variants last
- ||
- $a cmp $b } @thevalues;
- if ( scalar @thevalues >= 1 ){
- return "$thevalues[0]";
- } else {
- return;
- }
- }
- }
- }
- }
-
- # -------------------------------------------------------------
- sub guesslang (){
- # -------------------------------------------------------------
- # Try different combinations of $language and $country and possible
- # fallbacks in case extractlangname() does not find a good guess
- # -------------------------------------------------------------
- my $class = shift;
- my $language = shift;
- my $country = shift;
- my $guessed = '';
- my @possible_fallbacks = ();
-
- if ( $guessed = &extractlangname("$language" . "_" . uc($country))
- || &extractlangname("$language" . "_" . uc("$country") . ":1")
- || &extractlangname("$language")
- || &extractlangname("$language:1")
- ){
- $priority{$class} = "low";
- return $guessed;
- } else {
- @possible_fallbacks = grep {/$language\_/} sort keys %equivs;
- $priority{$class} = "medium";
- foreach ( @possible_fallbacks ){
- return $guessed if ( $guessed = &extractlangname($_));
- }
- $priority{$class} = "high";
- return;
- }
- }
-
- # -------------------------------------------------------------
- sub guessotherlang (){
- # -------------------------------------------------------------
- # Iterate over the not yet tried $langkey values for a dict to be
- # installed. Return first match
- # -------------------------------------------------------------
- my $guessed = '';
-
- foreach ( sort keys %pending_keys ){
- return $guessed if ( $guessed = &extractlangname($_));
- }
- }
-
- # -------------------------------------------------------------
- sub guesslang4link(){
- # -------------------------------------------------------------
- # Try guessing default value after (woody or older) former symlink
- # -------------------------------------------------------------
- my $class = shift;
- my $prefix = '';
- my $guess = '';
- my $language = '';
- my $link = "/etc/alternatives/$alternatives{$class}";
-
- return if not ( -l $link );
-
- if ( $guess = readlink($link) ){
-
- &dc_debugprint("Found link $guess. ");
-
- $guess =~ s/\.hash$//;
- $guess =~ s/^.*\///;
- $guess =~ s/(\-\.)(small|medium|large)$//;
- $guess =~ s/\-english$//;
-
- $guess = "norwegian->bokmal" if ($guess eq "bokmσl");
- $guess = "norwegian->nynorsk" if ($guess eq "nynorsk");
- $guess = "ogerman" if ($guess eq "german");
- $guess = "miscfiles" if ($guess eq "web2");
- $guess = "danish" if ($guess eq "dansk");
- $guess = "french" if ($guess eq "francais");
- $guess = "swedish" if ($guess eq "svenska");
-
- &dc_debugprint("Fine tuned to $guess.\n");
-
- if ( exists $reverse_equivs{$guess} ){
- $language = $reverse_equivs{$guess};
- } else {
- return;
- }
- return $guessed if ( $guessed = &extractlangname($language) );
- }
- return;
- }
-
- # -------------------------------------------------------------
- sub dc_manual_alternative (){
- # -------------------------------------------------------------
- # Check if woody (or older) alternative exists and is set to manual
- # -------------------------------------------------------------
- my $class = shift;
- my $file = "/var/lib/dpkg/alternatives/$alternatives{$class}";
- my $status = '';
-
- if ( -r $file ){
- open(FILE,"< $file") or return;
- $status = <FILE>;
- close FILE;
- $status = "" unless $status;
- chomp $status;
- return "Manual (previous alternative setting)" if ( $status eq "manual" );
- }
- }
-
- # -------------------------------------------------------------
- sub dc_debconf_rebuild (){
- # -------------------------------------------------------------
- # Gather info from debconf for the (to be) installed packages for class
- # %debconf_vals : pkg -> languages provided by package
- # %debconf_defaultvals : pkg -> default language for package
- # -------------------------------------------------------------
- my $class = shift;
- return unless $class;
- my $question = "shared/packages-$class";
- my ($errorcode,$pkgowners) = metaget ($question, "owners");
- return if $errorcode;
-
- %debconf_vals = ();
- %debconf_defaultvals = ();
- foreach ( split (/\s*,\s*/,$pkgowners) ){
- #$debconf_vals{$_} = metaget ("$_/languages", "default");
- $debconf_vals{$_} = get ("$_/languages");
- my ($errorcode,$pkgdefaults) = get ("$_/defaults");
- $debconf_defaultvals{$_} = $pkgdefaults if not $errorcode;
- }
- return "ok";
- }
-
- # -----------------------------------------------------------------
-
- &dc_debugprint("dictionaries-common: (re)configuring ...\n");
-
- if ( not -e $dcscript ){
- if ( -e "/etc/default/locale" ){
- $language = $ENV{'LANG'} if exists $ENV{'LANG'};
- }
- &dc_debugprint("LANG is set to $language\n") if $language;
- unless ( $language ){
- ($errorcode,$language) = get($di_language);
- $language = '' if $errorcode;
- &dc_debugprint("Debconf gives language: $language\n") if $language;
- }
- unless ( readlink ("/etc/alternatives/ispell-dictionary.hash") ||
- readlink ("/etc/alternatives/dictionary")){
- $language = $language ||
- $ENV{'LANG'} ||
- $ENV{'LC_MESSAGES'} ||
- $ENV{'LC_ALL'} ||
- '';
- }
- if ( $language ){ # Installing from scratch
- $language = "en" if ( $language eq "C" );
- # Deal with de_DE:de_DE@euro:de:en_GB.UTF-8:en like entries
- $language = ( split(":",$language) )[0];
- $language =~ s/[\.@].*$//; # Remove variant and charset
- ($language,$country) = split("_",$language);
- if ( not $country ){
- ($errorcode,$country) = get($di_country);
- if ( $errorcode or not $country ){
- $country = "unset";
- }
- }
- foreach $class ("ispell","wordlist"){
- $classprefix = substr($class,0,1);
- if ( &dc_debconf_rebuild($class) ){
- %pending_keys = %equivs;
- if ( $guessed = &guesslang($class,$language,$country) ){
- &dc_debugprint("* Guessed [d-i]->($class,$language,$country)\n");
- &dc_set("dictionaries-common/default-$class","$guessed");
- } else {
- if ( $guessed = &guesslang($class,"en","US")
- || &guessotherlang ){
- &dc_debugprint("*** Forcing [$guessed] for ($class,$language,$country) ***\n");
- $priority{$class} = "medium";
- &dc_set("dictionaries-common/default-$class","$guessed");
- &dc_debugprint("** --- **\n");
- } else {
- &dc_debugprint("* Nothing found\n");
- $priority{$class} = "critical";
- }
- }
- }
- }
- } else { # Upgrading from woody or previous release
- foreach ( keys %equivs ){
- $reverse_equivs{$equivs{$_}} = $_;
- }
- &dc_debugprint("dictionaries-common: Trying pre-sarge symlinks\n");
- foreach $class ("ispell","wordlist"){
- $classprefix = substr($class,0,1);
- if ( &dc_debconf_rebuild($class) ){
- %pending_keys = %equivs;
- if ( $guessed = &guesslang4link($class) ){
- if ( &dc_manual_alternative($class) ){
- &dc_debugprint("- $class was in manual mode. Setting critical priority\n");
- $priority{$class} = "critical";
- } else {
- $priority{$class} = "low";
- foreach ( keys %debconf_vals ){
- my $oldpackage = $_;
- next if ( $oldpackage eq "dictionaries-common" );
- $oldpackage = "wenglish" if ( $oldpackage eq "wamerican" );
- # critical priority if exists debconf entry without a
- # previous package installed. This means that besides
- # upgrading, new dicts are being installed.
- if ( not -e "/var/lib/dpkg/info/$oldpackage.list" ){
- $priority{$class} = "critical";
- &dc_debugprint("* New dict [$oldpackage] is to be installed\n");
- last;
- }
- }
- }
- &dc_set("dictionaries-common/default-$class","$guessed");
- }
- }
- }
- }
- }
-
- # Unregistering no longer used dictionaries-common/languages and
- # dictionaries-common ownership of other two shared questions
-
- unregister("dictionaries-common/languages");
- unregister("shared/packages-ispell");
- unregister("shared/packages-wordlist");
-
- # Prompting the questions if required
-
- if ( not -e $dcscript ){ # First dictionaries-common installation
- foreach $class ("ispell","wordlist"){
- &dc_debconf_select($class,$priority{$class});
- # This might have been pre-seeded and question not asked.
- # Make sure question is tagged as seen in this case
- if ( $priority{$class} ne "critical" ){
- fset ("dictionaries-common/default-$class", "seen", "true");
- go();
- }
- }
- } else { # Reconfiguring or upgrading
- foreach $class ("ispell","wordlist"){
- &dc_debconf_select($class);
- }
- }
-
- &dc_debugprint("dictionaries-common: (re)configuring ...Done.\n");
-
- # Local Variables:
- # perl-indent-level: 2
- # coding: iso-8859-1
- # End:
-
- # -------------------------------------------------------------------------------
- # dc-debconf-select.pl:
- # This file will be added to end of dictionaries-common.config-base
- # to make dictionaries-common.config, as well as installed under
- # /usr/share/dictionaries-common for single ispell dicts /wordlists use
- # -------------------------------------------------------------------------------
-
- sub dc_debconf_select (){
- my $class = shift;
- my $priority = shift;
- my $question = "shared/packages-$class";
- my $debug = "yes" if exists $ENV{'DICT_COMMON_DEBUG'};
- my @newchoices = ();
- my @oldchoices = ();
- my %title = ('ispell' => "Dictionaries-common: Ispell dictionary",
- 'wordlist' => "Dictionaries-common: Wordlist dictionary"
- );
-
- $priority = "medium" unless $priority;
-
- my ($errorcode,$pkgowners) = metaget ($question, "owners");
- return if $errorcode;
-
- foreach (split (/\s*,\s*/, $pkgowners)){
- my $entry = metaget ("$_/languages", "default");
- for ( $entry ){ # trim leading/trailing whitespaces
- s/^\s+//;
- s/\s+$//;
- }
- push (@newchoices, split(/\s*,\s*/, $entry));
- }
- my $choices = join (', ', sort {lc $a cmp lc $b} @newchoices);
-
- $question = "dictionaries-common/default-$class";
- @oldchoices = split(/\s*,\s*/,metaget ($question, "choices"));
- pop @oldchoices;
- my $oldchoices = join (', ', @oldchoices);
- print STDERR "** dictionaries-common: $class, $priority, $question\n" .
- " new:[$choices]\n old:[$oldchoices]\n" if $debug;
- unless ( scalar @newchoices == 0 ) {
- if ( $choices ne $oldchoices ) {
- subst ($question, "choices", $choices);
- fset ($question, "seen", "false");
- }
- input ($priority, $question);
- title ($title{$class});
- go ();
- }
-
- # If called from dictionaries-common.config, check actual values in debug mode
- if ( $debug && $fromdcconfig ){
- print STDERR "** dictionaries-common.config: Checking some real values for $question\n";
- print STDERR " Real choices: " . metaget ($question, "choices") . "\n";
- print STDERR " Real value: " . get ($question) . "\n";
- print STDERR "---\n";
- }
- }
-
- # Local Variables:
- # perl-indent-level: 2
- # End:
-
- 1;
-
-